package sample.asphalt2.control
{
	import flash.events.MouseEvent;
	import flash.utils.describeType;
	import flash.utils.getDefinitionByName;
	
	import asphalt2.control.AbstractControl;
	import asphalt2.event.PageContainerEvent;
	
	import flash.events.MouseEvent;
	
	import mx.core.UITextField;
	
	import sample.MenuPage;
	import sample.MonthlyPersonalWorkListPage;
	import sample.ProjectListPage;
	import sample.AnalyzePage;
	import sample.CostListPage;
	
	public class MenuControl extends AbstractControl
	{
		[Bindable]
		private var dataTree:XMLList=
				<>
			        <node label="プロジェクト">
		                <node label="プロジェクト一覧" page={describeType(ProjectListPage).@name}/>
		            </node>
		            <node label="予算">
		                <node label="経費一覧" page={describeType(CostListPage).@name}/>
		            </node>
			        <node label="実績">
		                <node label="個人工数一覧" page={describeType(MonthlyPersonalWorkListPage).@name}/>
		            </node>
			        <node label="分析">
		                <node label="分析" page={describeType(AnalyzePage).@name}/>
		            </node>
				</>

		override public function setup():void{
			var page:MenuPage = MenuPage(UI);		
			page.menuTree.dataProvider = dataTree;
			page.menuTree.labelField="@label"
			page.addEventListener(MouseEvent.CLICK,handleTreeClick);
		}
		public function handleTreeClick(e:MouseEvent):void{
			if(e.target is UITextField){
				var text:String = UITextField(e.target).text;
				var pageFQCN:String = dataTree.*.(@label==text).@page;
				if(pageFQCN != null && pageFQCN.length > 0){
					var pageClass:Class = getDefinitionByName(pageFQCN) as Class;
					UI.dispatchEvent(new PageContainerEvent("contentsPageContainer",pageClass));
				}
			}
		}
	}
}